<template>
	<view>
		<top-navbar title="我的订单" :isBack="!isHome" :isHome="isHome"></top-navbar>
		<view class="my-order">
			<view class="nav acea-row row-around" :style="{top: sysHeight + 'px'}">
				<view 
					v-for="(item,index) in tabList"
					:key="index"
					class="item" 
					:class="orderStatus == item.status ? 'on' : ''" 
					@click="statusClick(item.status)">
					<view>{{item.name}}</view>
					<!-- <view class="num">{{ orderData.unpaid_count || 0 }}</view> -->
				</view>
			</view>
			<view class="list">
				<view class="item" v-for="(item, index) in orderList" :key="index">
					<view @click="goOrderDetails(item.order_id)">
						<view class="title acea-row row-between-wrapper">
							<view class="acea-row row-middle">
								<text class="sign cart-color acea-row row-center-wrapper"
									v-if="item.bargain_id != 0">砍价</text>
								<text class="sign cart-color acea-row row-center-wrapper"
									v-else-if="item.combination_id != 0">拼团</text>
								<text class="sign cart-color acea-row row-center-wrapper"
									v-else-if="item.seckill_id != 0">秒杀</text>
								<text v-else>{{ item.module_type == 2 ? '门店' : '商城'}}</text>
								<text class="font-color-green" v-if="item.shipping_type == 2 && item.module_type == 1">-自提</text>
								<text style="font-size: 24rpx; color: #999; margin-left: 10rpx;">{{ item._add_time }}</text>
								<text class="gift" v-if="item.transfer_id > 0">礼物</text>
							</view>
							<view v-if="item._status._type == 0" class="font-color">待支付</view>
							<view v-else-if="item._status._type == 1 && item.shipping_type == 1 && item.combination_id == 0" class="font-color-green">
								待配送
							</view>
							<view v-else-if="item._status._type == 2 && item.shipping_type == 1" class="font-color-green">
								配送中
							</view>
							<view v-else-if="item._status._type == 3" class="font-color-green">
								待评价
							</view>
							<view v-else-if="item._status._type == 4">
								已完成
							</view>
							<view v-else-if="item._status._type == 1 && item.module_type == 2">
								已完成
							</view>
							<view v-else-if="item._status._type == 9" class="font-color-gray">
								订单已取消
							</view>
							<view v-else class="font-color-green">
								{{ item._status._title }}
							</view>
						</view>
						<view class="item-info acea-row row-between row-top" v-for="(itemn, ind) in item.cartInfo"
							:key="ind">
							<view class="pictrue">
								<image :src="itemn.productInfo.image" v-if="itemn.productInfo"></image>
								<image :src="itemn.image" v-else></image>
							</view>
							<view class="">
								<view class="text acea-row row-between" v-if="itemn.productInfo">
									<view class="name line2">{{ itemn.productInfo.store_name }}</view>
									<view class="money">
										<view v-if="itemn.productInfo.attrInfo">
											￥{{ itemn.productInfo.attrInfo.price }}</view>
										<view v-else>￥{{ itemn.productInfo.price }}</view>
										<view>x{{ itemn.cart_num }}</view>
									</view>
								</view>
								<view class="text acea-row row-between" v-else style="margin-bottom: 30rpx;">
									<view class="name line2">{{ itemn.title }}</view>
									<view class="money">
										<view v-if="itemn.price">￥{{ itemn.price }}</view>
										<view></view>
									</view>
								</view>
								<!-- 门店商品 -->
								<view 
									v-if="itemn.shopProductInfo && item.module_type == 2" 
									style="font-size: 24rpx; color: #999;">
									{{ itemn.shopAttrInfo.suk }}
								</view>
								<!-- 商城商品 -->
								<view
									class="line1"
									v-if="itemn.productInfo && item.module_type == 1" 
									style="width: 460rpx; font-size: 24rpx; color: #999;">
									<text v-if="itemn.productInfo.store_type == 2">
									{{ getSukText(itemn.productInfo.attrInfo.suk) }}，{{ getWeek(itemn.delivery_week) }}收花（{{ getWeekTimes(itemn.delivery_interval) }}）
									</text>
									<text v-else-if="itemn.productInfo.store_type == 3">{{ itemn.receive_time.days + ' ' + itemn.receive_time.name }} 送达</text>
									<text v-else>{{ itemn.productInfo.attrInfo.suk }}</text>
								</view>
								<!-- 券包 -->
								<view class="acea-row row-between" style="font-size: 24rpx; color: #999;" v-if="itemn.couponData">
									<text>包含{{ itemn.couponData.length }}张券</text>
									<text>共1件</text>
								</view>
							</view>
						</view>
						<view class="totalPrice">
							共{{ item.cartInfo.length || 0 }}件商品，总金额
							<text class="money font-color">￥{{ item.pay_price }}</text>
						</view>
					</view>
					<view class="bottom acea-row row-right row-middle">
						<view class="bnt cancelBnt" v-if="item._status._type == 0"
							@click="cancelOrder(index, item.order_id)">取消订单</view>
						<view class="bnt bg-color" v-if="item._status._type == 0"
							@click="goPay(item.pay_price, item.order_id)">立即付款</view>
						<view class="bnt bg-color" v-else-if="item._status._type == 3"
							@click="goOrderDetails(item.order_id)">去评价</view>
						<view class="bnt bg-color"
							v-else-if="item.seckill_id < 1 && item.bargain_id < 1 && item.combination_id < 1 && item._status._type == 4"
							@click="goOrderDetails(item.order_id)">
							再次购买
						</view>
						
						<view class="bnt cancelBnt" v-if="item._status._type == 4"
							@click="delOrder(item.order_id, index)">删除订单</view>
						<view class="bnt cancelBnt" 
							@click="goOrderDetails(item.order_id)">查看详情</view>
						<view class="bnt bg-color"
							style="width: fit-content; padding: 0 30rpx;"
							v-if="item._status._type == 11"
							@click="remainPay(item.remain_price, item.order_id)">支付尾款￥{{ item.remain_price }}元</view>
					</view>
				</view>
			</view>
			<view class="loadingicon acea-row row-center-wrapper" v-if="orderList.length > 0">
				<text class="loading iconfont icon-jiazai" :hidden="loading == false"></text>
				{{ loadTitle }}
			</view>
			<view v-if="orderList.length == 0">
				<emptyPage title="暂无订单~"></emptyPage>
			</view>
		</view>
		<!-- <view class="noCart" v-if="orderList.length == 0 && page > 1">
			<view class="pictrue">
				<image src="/static/images/noOrder.png"></image>
			</view>
		</view> -->
		<!-- #ifdef MP -->
		<!-- <authorize @onLoadFun="onLoadFun" :isAuto="isAuto" :isShowAuth="isShowAuth" @authColse="authColse"></authorize> -->
		<!-- #endif -->
		<!-- <home></home> -->
		<payment :isRemain="remain" :payMode="payMode" :pay_close="pay_close" @onChangeFun="onChangeFun" :order_id="pay_order_id"
			:totalPrice="totalPrice"></payment>
	</view>
</template>

<script>
	// #ifdef APP-PLUS || MP
	let sysHeight = uni.getSystemInfoSync().statusBarHeight + 43;
	// #endif
	// #ifndef APP-PLUS || MP
	let sysHeight = 0;
	// #endif
	import {
		getOrderList,
		orderData,
		orderCancel,
		orderDel,
		orderPay
	} from '@/api/order.js';
	import {
		getUserInfo
	} from '@/api/user.js';
	import {
		openOrderSubscribe
	} from '@/utils/SubscribeMessage.js';
	import home from '@/components/home';
	import payment from '@/components/payment';
	import navBar from '@/components/top-navbar/top-navbar'
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from 'vuex';
	// #ifdef MP
	import authorize from '@/components/Authorize';
	// #endif
	import emptyPage from '@/components/emptyPage.vue';
	export default {
		components: {
			payment,
			home,
			emptyPage,
			navBar,
			// #ifdef MP
			authorize
			// #endif
		},
		data() {
			return {
				sysHeight: sysHeight,
				loading: false, //是否加载中
				loadend: false, //是否加载完毕
				loadTitle: '加载更多', //提示语
				orderList: [], //订单数组
				orderData: {}, //订单详细统计
				orderStatus: '', //订单状态
				tabList:[ // status  ''=>全部，0=待付款，1=待发货，2=待收货，3=待评价，4=已完成，-4=已取消  99=服务中（含待配送跟配送中）
					{name: '全部', status: ''},
					{name: '待支付', status: '0'},
					{name: '服务中', status: '99'},
					// {name: '配送中', status: '2'},
					{name: '已完成', status: '4'},
					{name: '已取消', status: '-4'}
				],
				page: 1,
				limit: 20,
				payMode: [{
						name: '微信支付',
						icon: 'icon-weixinzhifu',
						value: 'weixin',
						title: '使用微信快捷支付',
						payStatus: true
					},
					{
						name: '支付宝支付',
						icon: 'icon-zhifubao',
						value: 'alipay',
						title: '使用线上支付宝支付',
						payStatus: true
					},
					{
						name: '余额支付',
						icon: 'icon-yuezhifu',
						value: 'yue',
						title: '当前可用余额：',
						number: 0,
						payStatus: true
					}
				],
				pay_close: false,
				pay_order_id: '',
				totalPrice: '0',
				isAuto: false, //没有授权的不会自动授权
				isShowAuth: false ,//是否隐藏授权
				isHome: false,
				remain: false
			};
		},
		computed: mapGetters(['isLogin']),
		onShow() {
			if (this.isLogin) {
				this.getOrderData();
				this.getOrderList();
				this.getUserInfo();
			} else {
				toLogin();
			}
		},
		onLoad(options) {
			if (options.status) this.orderStatus = options.status;
			if (options.home) this.isHome = options.home;
		},
		methods: {
			getSukText(suk) {
				let arr = suk.split('/')
				if(arr.length > 1) {
					if(arr[0] == '月') {
						return '1个月' + arr[1]
					} else if(arr[0] == '季') {
						return '1季度' + arr[1]
					} else if(arr[0] == '年') {
						return '1年' + arr[1]
					} else {
						return arr[0] + arr[1]
					}
				} else {
					return suk
				}
			},
			getWeekTimes(key) {
				let arr = ['', '一周一次', '两周一次']
				return arr[key]
			},
			getWeek(week){
				let data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
				let weeks = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
				let index = weeks.findIndex(item => item == week)
				return data[index]
			},
			onLoadFun() {
				this.getOrderData();
				this.getOrderList();
				this.getUserInfo();
			},
			// 授权关闭
			authColse: function(e) {
				this.isShowAuth = e;
			},
			/**
			 * 事件回调
			 *
			 */
			onChangeFun: function(e) {
				let opt = e;
				console.log(opt)
				let action = opt.action || null;
				let value = opt.value != undefined ? opt.value : null;
				action && this[action] && this[action](value);
			},
			/**
			 * 获取用户信息
			 *
			 */
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
					that.payMode[2].number = res.data.now_money;
					that.$set(that, 'payMode', that.payMode);
				});
			},
			/**
			 * 关闭支付组件
			 *
			 */
			payClose: function() {
				this.pay_close = false;
			},
			/**
			 * 生命周期函数--监听页面加载
			 */
			
			/**
			 * 获取订单统计数据
			 *
			 */
			getOrderData: function() {
				let that = this;
				orderData().then(res => {
					that.$set(that, 'orderData', res.data);
					that.payMode.map(item => {
						if (item.value == 'weixin') {
							item.payStatus = res.data.pay_weixin_open ? true : false;
						}
						if (item.value == 'alipay') {
							item.payStatus = res.data.ali_pay_status ? true : false;
						}
						if (item.value == 'yue') {
							item.payStatus = res.data.yue_pay_status == 1 ? true : false;
						}
					});
					//#ifdef MP
					this.payMode[1].payStatus = false;
					//#endif
				});
			},
			/**
			 * 取消订单
			 *
			 */
			cancelOrder: function(index, order_id) {
				let that = this;
				if (!order_id)
					return that.$util.Tips({
						title: '缺少订单号无法取消订单'
					});
				orderCancel(order_id)
					.then(res => {
						return that.$util.Tips({
								title: res.msg,
								icon: 'success'
							},
							function() {
								that.orderList.splice(index, 1);
								that.$set(that, 'orderList', that.orderList);
								that.$set(that.orderData, 'unpaid_count', that.orderData.unpaid_count - 1);
								that.getOrderData();
							}
						);
					})
					.catch(err => {
						return that.$util.Tips({
							title: err
						});
					});
			},
			/**
			 * 打开支付组件
			 *
			 */
			goPay: function(pay_price, order_id) {
				this.$set(this, 'remain', false);
				this.$set(this, 'pay_close', true);
				this.$set(this, 'pay_order_id', order_id);
				this.$set(this, 'totalPrice', pay_price);
			},
			// 支付尾款
			remainPay: function(pay_price, order_id) {
				this.$set(this, 'pay_close', true);
				this.$set(this, 'remain', true);
				this.$set(this, 'pay_order_id', order_id);
				this.$set(this, 'totalPrice', pay_price);
			},
			/**
			 * 支付成功回调
			 *
			 */
			pay_complete: function(isSplit) {
				this.loadend = false;
				this.page = 1;
				this.$set(this, 'orderList', []);
				this.pay_close = false;
				uni.navigateTo({
					url: '/pages/order_pay_status/index?order_id=' + this.pay_order_id +
						'&msg=支付成功&type=3&totalPrice=' + this.totalPrice + '&isSplit=' + isSplit
				})
				this.pay_order_id = '';
				this.getOrderData();
				this.getOrderList();
			},
			/**
			 * 支付失败回调
			 *
			 */
			pay_fail: function() {
				this.pay_close = false;
				this.pay_order_id = '';
			},
			/**
			 * 去订单详情
			 */
			goOrderDetails: function(order_id) {
				
				if (!order_id)
					return that.$util.Tips({
						title: '缺少订单号无法查看订单详情'
					});
				// #ifdef MP
				uni.showLoading({
					title: '正在加载'
				});
				openOrderSubscribe()
					.then(() => {
						uni.hideLoading();
						uni.navigateTo({
							url: '/pages/users/order_details/index?order_id=' + order_id
						})
					})
					.catch(err => {
						uni.hideLoading();
					});
				// #endif
				// #ifndef MP
				uni.navigateTo({
					url: '/pages/users/order_details/index?order_id=' + order_id
				});
				// #endif
			},
			/**
			 * 切换类型
			 */
			statusClick: function(status) {
				if (status == this.orderStatus) return;
				this.orderStatus = status;
				this.loadend = false;
				this.page = 1;
				this.$set(this, 'orderList', []);
				this.getOrderList();
			},
			/**
			 * 获取订单列表
			 */
			getOrderList: function() {
				let that = this;
				if (that.loadend) return;
				if (that.loading) return;
				that.loading = true;
				that.loadTitle = '加载更多';
				getOrderList({
						status: that.orderStatus,
						page: that.page,
						limit: that.limit
					})
					.then(res => {
						let list = res.data || [];
						let loadend = list.length < that.limit;
						that.orderList = that.$util.SplitArray(list, that.orderList);
						that.$set(that, 'orderList', that.orderList);
						that.loadend = loadend;
						that.loading = false;
						that.loadTitle = loadend ? '我也是有底线的' : '加载更多';
						that.page = that.page + 1;
					})
					.catch(err => {
						that.loading = false;
						that.loadTitle = '加载更多';
					});
			},

			/**
			 * 删除订单
			 */
			delOrder: function(order_id, index) {
				let that = this;
				orderDel(order_id)
					.then(res => {
						if(res.status == 200) {
							that.orderList.splice(index, 1);
							that.$set(that, 'orderList', that.orderList);
							that.$set(that.orderData, 'unpaid_count', that.orderData.unpaid_count - 1);
							that.getOrderData();
							return that.$util.Tips({
								title: '删除成功',
								icon: 'success'
							});
						} else {
							return that.$util.Tips({
								title: res.msg
							});
						}
						
					})
					.catch(err => {
						return that.$util.Tips({
							title: err
						});
					});
			}
		},
		onReachBottom: function() {
			this.getOrderList();
		}
	};
</script>

<style scoped lang="scss">
	.my-order .header {
		height: 260rpx;
		padding: 0 30rpx;
	}
	.font-color-green {
		color: #95c51b;
	}
	.font-color-gray {
		color: #999;
	}
	.my-order .header .picTxt {
		height: 190rpx;
	}
	.gift {
		background-color: #9AC83A;
		color: #fff;
		font-size: 24rpx;
		padding: 0 10rpx;
		border-radius: 8rpx;
		margin-left: 20rpx;
	}
	.my-order .header .picTxt .text {
		color: rgba(255, 255, 255, 0.8);
		font-size: 26rpx;
		font-family: 'Guildford Pro';
	}

	.my-order .header .picTxt .text .name {
		font-size: 34rpx;
		font-weight: bold;
		color: #fff;
		margin-bottom: 20rpx;
	}

	.my-order .header .picTxt .pictrue {
		width: 122rpx;
		height: 109rpx;
	}

	.my-order .header .picTxt .pictrue image {
		width: 100%;
		height: 100%;
	}

	.my-order .nav {
		position: sticky;
		background-color: #fff;
		width: 100%;
		height: 100rpx;
		border-radius: 6rpx;
		margin: 0 auto 0 auto;
		z-index: 99;
	}

	.my-order .nav .item {
		display: flex;
		align-items: center;
		height: 100%;
		text-align: center;
		font-size: 30rpx;
		color: #282828;
		// padding: 27rpx 0;
		border-bottom: 5rpx solid transparent;
	}

	.my-order .nav .item.on {
		/* #ifdef H5 || MP */
		font-weight: bold;
		/* #endif */
		/* #ifdef APP-PLUS */
		color: #000;
		/* #endif */
		border-color: #95c51b;
		color: #95c51b;
	}

	.my-order .nav .item .num {
		margin-top: 18rpx;
	}

	.my-order .list {
		width: 710rpx;
		margin: 14rpx auto 0 auto;
	}

	.my-order .list .item {
		background-color: #fff;
		border-radius: 10rpx;
		margin-bottom: 14rpx;
		border: 1px solid #eee;
	}

	.my-order .list .item .title {
		height: 84rpx;
		padding: 0 30rpx;
		border-bottom: 1rpx solid #eee;
		font-size: 28rpx;
		color: #282828;
	}

	.my-order .list .item .title .sign {
		font-size: 24rpx;
		padding: 0 7rpx;
		height: 36rpx;
		margin-right: 15rpx;
	}

	.my-order .list .item .item-info {
		padding: 0 30rpx;
		margin-top: 22rpx;
	}

	.my-order .list .item .item-info .pictrue {
		width: 120rpx;
		height: 120rpx;
	}

	.my-order .list .item .item-info .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 6rpx;
	}

	.my-order .list .item .item-info .text {
		width: 486rpx;
		font-size: 28rpx;
		color: #999;
		margin-top: -3rpx;
	}

	.my-order .list .item .item-info .text .name {
		width: 306rpx;
		color: #282828;
	}

	.my-order .list .item .item-info .text .money {
		text-align: right;
	}

	.my-order .list .item .totalPrice {
		font-size: 26rpx;
		color: #282828;
		text-align: right;
		margin: 27rpx 0 0 30rpx;
		padding: 0 30rpx 30rpx 0;
		border-bottom: 1rpx solid #eee;
	}

	.my-order .list .item .totalPrice .money {
		font-size: 28rpx;
		font-weight: bold;
	}

	.my-order .list .item .bottom {
		height: 107rpx;
		padding: 0 30rpx;
	}

	.my-order .list .item .bottom .bnt {
		width: 176rpx;
		height: 60rpx;
		text-align: center;
		line-height: 60rpx;
		color: #fff;
		border-radius: 50rpx;
		font-size: 27rpx;
	}

	.my-order .list .item .bottom .bnt.cancelBnt {
		border: 1rpx solid #ddd;
		color: #aaa;
	}

	.my-order .list .item .bottom .bnt~.bnt {
		margin-left: 17rpx;
	}

	.noCart {
		margin-top: 171rpx;
		padding-top: 0.1rpx;
	}

	.noCart .pictrue {
		width: 414rpx;
		height: 336rpx;
		margin: 78rpx auto 56rpx auto;
	}

	.noCart .pictrue image {
		width: 100%;
		height: 100%;
	}

	.line2 {
		word-break: break-all;
	}

	.bottom {
		.bg-green {
			background-color: #a0cd43;
		}
	}
</style>
